using System.Text.RegularExpressions;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class UI_CopyText : MonoBehaviour
{
	[SerializeField]
	private TextMeshProUGUI TargetText;

	[SerializeField]
	private TextMeshProUGUI thisText;

	[Range(0f, 1f)]
	[SerializeField]
	private float TextOpacity;

	[Header("-- Custom Colors --")]
	[SerializeField]
	private bool UseSpecificColor;

	[SerializeField]
	private Color SpecificColor;

	[Header("-- Additional Settings --")]
	[Header("True clone copies the target texts position, bounds and maxvisible characters")]
	[Header("Use sparingly, can be quite performance intenstive")]
	[SerializeField]
	private bool TrueClone;

	[SerializeField]
	private Vector3 Offset;

	[Header("Doesn't disable rich text itself, but removes the <> tags")]
	[SerializeField]
	private bool DisableRichTextTags;

	[SerializeField]
	private bool DarkenColorTags;

	[Range(0f, 1f)]
	[SerializeField]
	private float darkenAmount = 0.2f;

	private string[] preserveTags = new string[8] { "b", "color", "i", "u", "br", "scale", "lowercase", "uppercase" };

	private void LateUpdate()
	{
		//IL_0172: Unknown result type (might be due to invalid IL or missing references)
		//IL_0192: Unknown result type (might be due to invalid IL or missing references)
		//IL_0198: Unknown result type (might be due to invalid IL or missing references)
		//IL_019d: Unknown result type (might be due to invalid IL or missing references)
		//IL_013b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)TargetText != (Object)null && (Object)(object)thisText != (Object)null && ((Component)TargetText).gameObject.activeSelf && ((Behaviour)TargetText).enabled && ((Behaviour)thisText).enabled && ((TMP_Text)thisText).text != ((TMP_Text)TargetText).text)
		{
			if (DisableRichTextTags)
			{
				((TMP_Text)thisText).text = RemoveRichTextTags(((TMP_Text)TargetText).text, preserveTags);
			}
			else
			{
				((TMP_Text)thisText).text = ((TMP_Text)TargetText).text;
			}
			if (!UseSpecificColor)
			{
				((Graphic)thisText).color = new Color(((Graphic)TargetText).color.r, ((Graphic)TargetText).color.g, ((Graphic)TargetText).color.b, TextOpacity);
			}
			else
			{
				((Graphic)thisText).color = new Color(SpecificColor.r, SpecificColor.g, SpecificColor.b, SpecificColor.a);
			}
		}
		if (TrueClone)
		{
			((TMP_Text)thisText).maxVisibleCharacters = ((TMP_Text)TargetText).maxVisibleCharacters;
			((TMP_Text)thisText).margin = ((TMP_Text)TargetText).margin;
			((TMP_Text)thisText).transform.position = ((TMP_Text)TargetText).transform.position + Offset;
			((TMP_Text)thisText).fontSize = ((TMP_Text)TargetText).fontSize;
			((TMP_Text)thisText).font = ((TMP_Text)TargetText).font;
			((Behaviour)thisText).enabled = ((Behaviour)TargetText).enabled;
			((TMP_Text)thisText).styleSheet = ((TMP_Text)TargetText).styleSheet;
			((TMP_Text)thisText).textStyle = ((TMP_Text)TargetText).textStyle;
		}
	}

	public string RemoveRichTextTags(string input, string[] preserveTags)
	{
		string text = string.Join("|", preserveTags);
		string pattern = "<(?!/?(?:" + text + ")\\b).*?>";
		if (DarkenColorTags)
		{
			input = Regex.Replace(input, "<color=([^>]+)>", delegate(Match match)
			{
				//IL_0099: Unknown result type (might be due to invalid IL or missing references)
				string value = match.Groups[1].Value;
				Color val = default(Color);
				if (ColorUtility.TryParseHtmlString(value, ref val) || (value.StartsWith("#") && ColorUtility.TryParseHtmlString(value, ref val)) || (!value.StartsWith("#") && ColorUtility.TryParseHtmlString("#" + value, ref val)))
				{
					val.r *= 1f - darkenAmount;
					val.g *= 1f - darkenAmount;
					val.b *= 1f - darkenAmount;
					string text2 = ColorUtility.ToHtmlStringRGB(val);
					return "<color=#" + text2 + ">";
				}
				return match.Value;
			});
		}
		return Regex.Replace(input, pattern, "");
	}
}
